package com.podium.web.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.podium.auth.utils.SecurityUtils;
import com.podium.comment.utils.StringUtils;
import com.podium.web.system.entity.SysDictData;
import com.podium.web.system.entity.SysDictType;
import com.podium.web.system.mapper.SysDictDataMapper;
import com.podium.web.system.service.SysDictDataService;
import com.podium.web.system.service.SysDictTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 邵文泽
 * @description 字典数据
 * @date 2025-05-09 15:33:44
 */
@Slf4j
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements SysDictDataService {

    @Autowired
    private SysDictTypeService dictTypeService;

    @Override
    public IPage<SysDictData> page(Page<SysDictData> page, SysDictData sysDictData) {
        QueryWrapper<SysDictData> queryWrapper = new QueryWrapper<>();

        queryWrapper
                .lambda()
                .eq(SysDictData::getDelFlag, 0)
                .like(StringUtils.hasLength(sysDictData.getDictLabel()), SysDictData::getDictLabel, sysDictData.getDictLabel())
                .like(StringUtils.hasLength(sysDictData.getDictValue()), SysDictData::getDictValue, sysDictData.getDictValue())
                .orderByAsc(SysDictData::getSort)
                .orderByDesc(SysDictData::getId)
        ;
        Page<SysDictData> dataPage = super.page(page, queryWrapper);
        List<Long> dictTypeIds = dataPage.getRecords().stream()
                .map(SysDictData::getDictTypeId)
                .distinct()
                .toList();
        if (dictTypeIds.isEmpty()) {
            return dataPage;
        }
        List<SysDictType> dictTypes = dictTypeService.listByIds(dictTypeIds);
        Map<Long, String> dictTypeMap = dictTypes.stream().collect(Collectors.toMap(SysDictType::getId, SysDictType::getDictName));
        dataPage.getRecords().forEach(data -> data.setDictTypeName(dictTypeMap.getOrDefault(data.getDictTypeId(), "未知类型")));
        return dataPage;
    }

    @Override
    public List<SysDictData> list(SysDictData sysDictData) {
        QueryWrapper<SysDictData> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(SysDictData::getDelFlag, 0)
                .like(StringUtils.hasLength(sysDictData.getDictLabel()), SysDictData::getDictLabel, sysDictData.getDictLabel())
                .like(StringUtils.hasLength(sysDictData.getDictValue()), SysDictData::getDictValue, sysDictData.getDictValue())
                .orderByAsc(SysDictData::getSort)
                .orderByDesc(SysDictData::getId)
        ;
        return super.list(queryWrapper);
    }

    @Override
    public SysDictData getById(Long id) {
        return super.getById(id);
    }

    @Override
    public boolean save(SysDictData sysDictData) {
        sysDictData.setDelFlag(0L);
        sysDictData.setCreateBy(SecurityUtils.getUsername());
        sysDictData.setCreateTime(LocalDateTime.now());
        return super.save(sysDictData);
    }

    @Override
    public boolean updateById(SysDictData sysDictData) {
        sysDictData.setUpdateBy(SecurityUtils.getUsername());
        sysDictData.setUpdateTime(LocalDateTime.now());
        return super.updateById(sysDictData);
    }

    @Override
    public void removeById(Long id) {
        super.removeById(id);
    }

}